package com.zxbit.uangstore.main;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.WindowManager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.zxbit.uangstore.R;
import com.zxbit.uangstore.adapter.HistoryAdapter;
import com.zxbit.uangstore.base.BaseActivity;
import com.zxbit.uangstore.business.RouterMap;
import com.zxbit.uangstore.cache.UserInfo;
import com.zxbit.uangstore.constant.Constant;
import com.zxbit.uangstore.domain.BlankResponse;
import com.zxbit.uangstore.domain.ProductResponse;
import com.zxbit.uangstore.mvpview.MineMvpView;
import com.zxbit.uangstore.presenter.MinePresenter;
import com.zxbit.uangstore.utils.ToastUtils;
import com.zxbit.uangstore.view.NormalDialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import crossoverone.statuslib.StatusUtil;

@SuppressLint("CheckResult")
@Route(name = "我的", path = RouterMap.MINE_ACTIVITY_PAGE)
public class MineActivity extends BaseActivity implements MineMvpView, HistoryAdapter.OnHistoryListener {

    @BindView(R.id.recycler_view)
    RecyclerView historyRecycler;

    private HistoryAdapter historyAdapter;
    private NormalDialog mNormalDialog;

    private MinePresenter minePresenter;
    private String productId;

    @Override
    protected void setSystemInvadeBlack() {
        StatusUtil.setUseStatusBarColor(this, Color.parseColor("#00000000"));
        // 第二个参数是是否沉浸,第三个参数是状态栏字体是否为黑色。
        StatusUtil.setSystemStatus(this, true, true);
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this, this);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        historyRecycler.setLayoutManager(linearLayoutManager);
        historyRecycler.setAdapter(getRecyclerAdapter());
    }

    @Override
    protected void onUIReady() {
        minePresenter = new MinePresenter(this);

        if (UserInfo.isLogin()) {
            minePresenter.getHistory();
            showLoadingView();
        }
    }

    @Override
    protected int getContentResId() {
        return R.layout.layout_of_mine;
    }

    @Override
    protected int getResId() {
        return 0;
    }

    @OnClick({R.id.back_btn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                finish();
                break;
        }
    }

    public HistoryAdapter getRecyclerAdapter() {
        if (historyAdapter == null) {
            historyAdapter = new HistoryAdapter(this, this);
        }
        return historyAdapter;
    }

    private void showLogoutDialog() {
        if (mNormalDialog == null) {
            mNormalDialog = new NormalDialog(MineActivity.this, R.style.CustDialogStyle)
                    .setTitle(getResources().getString(R.string.tips))
                    .setContent(getResources().getString(R.string.is_delete_data))
                    .setNormalClickListener(new NormalDialog.NormalClickListener() {
                        @Override
                        public void onLeftClick() {
                            mNormalDialog.dismiss();
                            minePresenter.deleteHistory(productId);
                            showLoadingView();
                        }

                        @Override
                        public void onRightClick() {
                            mNormalDialog.dismiss();
                        }
                    });
        }
        WindowManager.LayoutParams params = mNormalDialog.getWindow().getAttributes();//获取dialog信息
        params.width = (int) getResources().getDimension(R.dimen.x660);
        mNormalDialog.getWindow().setAttributes(params);//设置大小

        mNormalDialog.show();
    }


    @Override
    public void onGetDataNext(ProductResponse productResponse) {
        dismissLoadingView();
        if (productResponse.getStatus() == Constant.HTTP_OK) {
            if (productResponse.hasData()) {
                getRecyclerAdapter().updateItems(productResponse.getData());
            } else {
                getRecyclerAdapter().removeAllItems();
            }
        } else {
            ToastUtils.showShort(MineActivity.this, productResponse.getMessage());
        }
    }

    @Override
    public void onGetDataNext(BlankResponse response) {
        if (response.getStatus() == Constant.HTTP_OK) {
            minePresenter.getHistory();
            showLoadingView();
        } else {
            dismissLoadingView();
            ToastUtils.showShort(MineActivity.this, response.getMessage());
        }
    }

    @Override
    public void onGetDataError(Throwable throwable) {
        dismissLoadingView();
    }

    @Override
    public void onHistoryClick(View view, String productId) {
        this.productId = productId;
        showLogoutDialog();
    }
}
